<?php
include('system/sql.php');
// 获取参数并过滤
$wbtreeid = isset($_GET['wbtreeid']) ? intval($_GET['wbtreeid']) : 0;
$page = isset($_GET['page']) ? max(1, intval($_GET['page'])) : 1;
$perPage = 10; // 每页显示10条

// 获取栏目名称
$menu_name = '';
$menu_query = mysqli_query($link, "SELECT menu FROM article_menu WHERE wbtreeid = $wbtreeid");
if ($menu_row = mysqli_fetch_assoc($menu_query)) {
    $menu_name = $menu_row['menu'];
}

// 获取总记录数
$count_query = mysqli_query($link, "SELECT COUNT(*) FROM article_article WHERE wbtreeid = $wbtreeid");
$total = mysqli_fetch_row($count_query)[0];
$totalPages = ceil($total / $perPage);

// 计算偏移量
$offset = ($page - 1) * $perPage;

// 获取当前页文章
$articles_query = mysqli_query(
    $link,
    "SELECT * 
     FROM article_article 
     WHERE wbtreeid = $wbtreeid 
     ORDER BY time DESC 
     LIMIT $offset, $perPage"
);
$webinfo_sql = "select * from webinfo";
$webinfo_stmt = $link->prepare($webinfo_sql);
$webinfo_stmt->execute();
$webinfo_result = $webinfo_stmt->get_result();
$webinfo_row = $webinfo_result->fetch_assoc();
?>
<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo $menu_name . " - " . $webinfo_row['webname']; ?></title>
    <!-- Bootstrap 5 CSS -->
    <link href="/static/bootstrap.min.css" rel="stylesheet">
    <!-- 自定义样式 -->
    <style>
        .article-list {
            list-style-type: none;
            padding-left: 0;
        }
        
        .article-bg-container {
            padding: 5rem;
            background-position: center;
            background-size: cover;
            min-height: 200px;
        }
        
        .default-bg {
            background-color: #f0f0f0;
        }
    </style>
</head>

<body>

<?php include('nav.php'); ?>

    <div class="article-bg-container default-bg">
        <div class="text-center">
            <h1><?= htmlspecialchars($menu_name) ?></h1>
        </div>
    </div>

    <div class="container my-5">
        <div class="row">
            <div class="col">
                <?php while ($article = mysqli_fetch_assoc($articles_query)): ?>
                    <?php
                    $content = strip_tags($article['content']);
                    $summary = mb_substr($content, 0, 50, 'UTF-8') . (mb_strlen($content) > 50 ? '...' : '');
                    $pub_date = date('Y-m-d H:i:s', $article['time']);
                    ?>
                    <div class="card mb-4 shadow-sm">
                        <div class="row g-0">
                            <?php
                            // Use regular expressions to match image tags and extract the first image URL
                            preg_match('/<img[^>]+src=["\']([^"\']+)["\']/i', $article['content'], $matches);
                            if (!empty($matches[1])) {
                                $firstImageUrl = $matches[1];
                            ?>
                                <div class="col-md-4">
                                    <img src="<?= htmlspecialchars($firstImageUrl) ?>" class="img-fluid rounded-start">
                                </div>
                            <?php
                            }
                            ?>
                            <div class="col-md-8">
                                <div class="card-body">
                                    <h5 class="card-title"><?= htmlspecialchars($article['title']) ?></h5>
                                    <p class="card-text text-muted"><?= $pub_date ?></p>
                                    <p class="card-text"><?= htmlspecialchars($summary) ?></p>

                                    <?php
                                    // 获取伪静态配置
                                    $is_static = isset($webinfo_row['static']) ? $webinfo_row['static'] : 0;

                                    // 生成最终链接（自动处理外链和伪静态）
                                    $final_link = empty($article['url'])
                                        ? ($is_static
                                            ? "/article/article-{$article['wbtreeid']}-{$article['id']}.htm"
                                            : "article.php?wbtreeid={$article['wbtreeid']}&id={$article['id']}")
                                        : $article['url'];
                                    ?>
                                    <a href="<?= htmlspecialchars($final_link) ?>"
                                        <?= !empty($article['url']) ? 'target="_blank"' : '' ?>
                                        class="btn btn-sm btn-primary">
                                        阅读全文
                                    </a>

                                </div>
                            </div>
                        </div>
                    </div>
                <?php endwhile; ?>

                <!-- 分页 -->

                <nav aria-label="Page navigation">
                    <ul class="pagination justify-content-center">
                        <li class="page-item <?= $page <= 1 ? 'disabled' : '' ?>">
                            <a class="page-link" href="<?= $webinfo_row['static'] ? '/list/list-' . $wbtreeid . '-' . ($page - 1) . '.htm' : '?wbtreeid=' . $wbtreeid . '&page=' . ($page - 1) ?>">上一页</a>
                        </li>

                        <?php for ($i = 1; $i <= $totalPages; $i++): ?>
                            <li class="page-item <?= $i == $page ? 'active' : '' ?>">
                                <a class="page-link" href="<?= $webinfo_row['static'] ? '/list/list-' . $wbtreeid . '-' . $i . '.htm' : '?wbtreeid=' . $wbtreeid . '&page=' . $i ?>"><?= $i ?></a>
                            </li>
                        <?php endfor; ?>

                        <li class="page-item <?= $page >= $totalPages ? 'disabled' : '' ?>">
                            <a class="page-link" href="<?= $webinfo_row['static'] ? '/list/list-' . $wbtreeid . '-' . ($page + 1) . '.htm' : '?wbtreeid=' . $wbtreeid . '&page=' . ($page + 1) ?>">下一页</a>
                        </li>
                    </ul>
                </nav>

            </div>
        </div>
    </div>

    <?php include('bottom.php');
    mysqli_close($link); ?>
</body>

</html>